	var insert_video = function(id, url, jstrack) {
		var response = '';
		var trackname = '';
		$.ajax({
			type: "GET",
			url: "http://swift-door-831.appspot.com/log?url=" + url,
			async: false,
			success: function(text) {
				response = text;
			}
		});
		
		var video = document.getElementById(id);
		var regyid= RegExp('(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})','i');
		var thumb_url = "http://i.ytimg.com/vi/%s/sddefault.jpg".replace("%s",regyid.exec(url)[1]);
		var source = document.createElement('source');
		source.setAttribute('src', response);
		//set thumb
			$("#"+id).attr("poster",thumb_url)
		video.appendChild(source);
		//insert track
		if (jstrack != "") {
			var track = document.createElement('script');
			track.setAttribute('src', jstrack);
			$('body').append(track);

		}
	}
	var getvideo = function() {
		$('video').each(function() {
			if ($(this).attr("data-video-url")) {
				var videourl = $(this).attr("data-video-url");
				if ($(this).attr("data-track-url")) {
					var trackurl = $(this).attr("data-track-url");
					insert_video($(this).attr("id"), videourl, trackurl);
				} else {
					var trackurl = "";
					insert_video($(this).attr("id"), videourl, trackurl);
				}

			} else {
				console.log("missing Video Url in %s", $(this).attr("id"));
			}

		})
	}
	
$(document).ready(function() {
	getvideo();

})
